package com.kanshu.ksgb.fastread.doudou.module.bookcity.view;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kanshu.ksgb.fastread.doudou.R;
import com.kanshu.ksgb.fastread.doudou.app.constants.ARouterConfig;
import com.kanshu.ksgb.fastread.doudou.app.constants.Constants;
import com.kanshu.ksgb.fastread.doudou.common.business.ad.presenter.AdPresenter;
import com.kanshu.ksgb.fastread.doudou.common.business.commonbean.BookInfo;
import com.kanshu.ksgb.fastread.doudou.common.imageloader.GlideImageLoader;
import com.kanshu.ksgb.fastread.doudou.common.supertextview.SuperTextView;
import com.kanshu.ksgb.fastread.doudou.common.util.ARouterUtils;
import com.kanshu.ksgb.fastread.doudou.common.util.DisplayUtils;
import com.kanshu.ksgb.fastread.doudou.common.business.utils.BookUtils;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.ItemRefreshBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.bean.SelectedBean;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.presenter.BookCityPresenter;
import com.kanshu.ksgb.fastread.doudou.module.bookcity.utils.BookCityUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.Subject;

import static com.kanshu.ksgb.fastread.doudou.base.baseui.SwipeBackActivity.ACTION_DESTORY;

/**
 * Created by wcy on 18/5/15.
 */

public class AdSelectedThreeLayout extends LinearLayout implements IRefresh<SelectedBean> {

    @BindView(R.id.label)
    TextView mLabel;
    ItemRefreshBean mItemRefreshBean;
    boolean mIsFirst;
    @BindView(R.id.change_tip)
    ImageView mChangeTip;
    public Subject<Integer> lifeCyclerSubject = PublishSubject.create();
    BookCityPresenter presenter;
    @BindView(R.id.top_container)
    LinearLayout mTopContainer;
    @BindView(R.id.bottom_container)
    LinearLayout mBottomContainer;

    public AdSelectedThreeLayout(Context context) {
        super(context);
        init();
    }

    public AdSelectedThreeLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public AdSelectedThreeLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        presenter = new BookCityPresenter(lifeCyclerSubject);
        setOrientation(VERTICAL);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.item_ad_selected_three_layout, this);
        ButterKnife.bind(view);
    }

    private void refreshItem(int pos, LinearLayout container, BookInfo item) {
        container.getChildAt(pos).setVisibility(VISIBLE);
        container.getChildAt(pos).setOnClickListener(v -> {
            ARouterUtils.toActivity(ARouterConfig.BOOK_DETAIL, "book_id", item.book_id);
            /**
             * 埋点
             */
            BookCityUtils.takeIf(mItemRefreshBean, Constants.BookCityModuleType.TYPE_SELECTED_WANJIEJINGDIAN, () -> {
                AdPresenter.pvuvStatics(mBottomContainer == container ? pos + 2 : pos,
                        R.string.SCTUIJIAN3_POSITION1,
                        R.string.SCTUIJIAN3_POSITION2,
                        R.string.SCTUIJIAN3_POSITION3,
                        R.string.SCTUIJIAN3_POSITION4);
            });
        });
        FrameLayout frameLayout = (FrameLayout) container.getChildAt(pos);
        RelativeLayout layout = (RelativeLayout) frameLayout.getChildAt(0);
        ImageView imageView = (ImageView) layout.getChildAt(0);
        LinearLayout linearLayout = (LinearLayout) layout.getChildAt(1);
        TextView tv1 = (TextView) linearLayout.getChildAt(0);
        LinearLayout linearLayout1 = (LinearLayout) linearLayout.getChildAt(1);
        SuperTextView tv2 = (SuperTextView) linearLayout1.getChildAt(0);
        ImageView tv3 = (ImageView) linearLayout1.getChildAt(1);
        GlideImageLoader.load(item.cover_url, imageView);
        tv1.setText(item.book_title);
        tv2.setText(BookUtils.getBookLabel(item));
        BookUtils.setStatusImgBg(tv3, item);
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        lifeCyclerSubject.onNext(ACTION_DESTORY);
    }

    @Override
    public void refresh(SelectedBean selectedBean, Object object) {
        if (selectedBean == null || object == null) {
            return;
        }
        BookCityUtils.rotateCancel(mChangeTip);
        mItemRefreshBean = (ItemRefreshBean) object;
        mLabel.setText(mItemRefreshBean.selectionName);
        mLabel.setCompoundDrawablesWithIntrinsicBounds(mItemRefreshBean.selectionIcon, 0, 0, 0);
        for (int i = 0; i < mTopContainer.getChildCount(); i++) {
            mTopContainer.getChildAt(i).setVisibility(GONE);
        }
        for (int i = 0; i < mBottomContainer.getChildCount(); i++) {
            mBottomContainer.getChildAt(i).setVisibility(GONE);
        }
        if (selectedBean.list == null) {
            return;
        }
        if (selectedBean.list.size() <= 2) {
            DisplayUtils.gone(mBottomContainer);
        } else {
            DisplayUtils.visible(mBottomContainer);
        }
        if (selectedBean.list.size() > 4) {
            selectedBean.list = selectedBean.list.subList(0, 4);
        }
        for (int i = 0; i < selectedBean.list.size(); i++) {
            if (i < 2) {
                refreshItem(i, mTopContainer, selectedBean.list.get(i));
            } else {
                refreshItem(i - 2, mBottomContainer, selectedBean.list.get(i));
            }
        }
    }

    @Override
    public void onError(int code, String desc) {
        BookCityUtils.rotateCancel(mChangeTip);
    }

    @Override
    public void reset() {

    }


    @OnClick(R.id.change_container)
    public void onViewClicked() {
        BookCityUtils.rotate(mChangeTip);
        BookCityUtils.refreshModuleInfos(mItemRefreshBean, this, presenter);
        /**
         * 埋点
         */
        BookCityUtils.takeIf(mItemRefreshBean, Constants.BookCityModuleType.TYPE_SELECTED_WANJIEJINGDIAN, () -> {
            AdPresenter.pvuvStatics(R.string.SCTUIJIAN3_HUANYIHUAN);
        });
    }
}
